<!--
  功能：审核大厅
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月07日 15:08:57
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='student_apply'>
    <div style="text-align:center">
      <el-form inline :model="param">
        <el-form-item>
          <el-select v-model="param.year" placeholder="年度" style="width:100px" clearable>
            <el-option v-for="year in yearOptions" 
              :key="year" 
              :label="year" 
              :value="year">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-cascader filterable v-model="param.clazzId" clearable :options="directionClazzTree" placeholder="请选择班级"
          :props="{ children:'clazzes',label:'name',value:'id',emitPath:false}" ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" plain  icon="el-icon-search" @click="loadApplies">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="btns" style="text-align:right">
      <el-button size="small" type="primary" @click="toCheckHandler()">批量审核</el-button>
    </div>
    <el-table size="small" :data="applyData.list"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" :selectable="item=>item.status === '未审核'" width="55" align="center" fixed="left" > </el-table-column>
      <el-table-column type="index" :index="1" label="序号" align="center"></el-table-column>
      <el-table-column label="班级" width="190" align="center" fixed="left">
        <template slot-scope="scope">
          {{scope.row.student.clazz}}
        </template>
      </el-table-column>
      <el-table-column prop="student.realname" label="姓名" width="90" align="center" fixed="left"/>
      <el-table-column label="申请类型" prop="type" width="80" align="center"></el-table-column>
      <el-table-column label="申请时间" align="center" width="100">
        <template slot-scope="scope">
            {{scope.row.applyTime | fmtDate}}
          </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" width="100">
        <template slot-scope="scope">
            {{scope.row.beginTime | fmtDate}}
          </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" width="100">
         <template slot-scope="scope">
            {{scope.row.finishTime | fmtDate}}
          </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="80">
         <template slot-scope="scope">
            <el-tag size="mini" type="warning" v-if="scope.row.status === '未审核'">{{scope.row.status}}</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.status === '审核通过'">{{scope.row.status}}</el-tag>
            <el-tag size="mini" type="success" v-else>{{scope.row.status}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="事由" prop="reason" align="left" min-width="300"></el-table-column>
      <el-table-column label="备注" prop="comment" align="center" width="150"></el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="toCheckHandler(scope.row)" :disabled="scope.row.status !== '未审核'">审核</el-button>
          <el-button type="text" size="mini" @click="toCheckHandler(scope.row,true)" >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
     <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="applyData.total" 
          :page-size="applyData.pageSize" 
          :current-page="applyData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
    <!-- 图片预览框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="40%">
      <div>
        <img src="" alt="">
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 图片预览框 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" title="审核" width="50%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <div class="sub_title">请假信息</div>
        <el-table size="small" :data="applies">
          <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
          <el-table-column label="请假信息" align="center" width="300">
            <template slot-scope="scope">
              <div>
                {{scope.row.student.realname}}
              </div>
              <div>
                {{scope.row.beginTime | fmtDate}} ~ {{scope.row.finishTime | fmtDate}}
              </div>
              <div>
                {{scope.row.comment}}
              </div>
              </template>
          </el-table-column>
          <el-table-column label="假条照片" align="center" >
            <template slot-scope="scope">
              <div class="photo_container">
                <el-image style="width: 100%; " :src="scope.row.leavePaper" :preview-src-list="[scope.row.leavePaper]"> </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="请假凭证" align="center" >
            <template slot-scope="scope">
              <div  class="photo_container">
                <el-image v-if="scope.row.scrip" style="width: 100%;" :src="scope.row.scrip" :preview-src-list="[scope.row.scrip]"> </el-image>
                <span v-else>未上传</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="sub_title"  v-if="!onlyBrowse" style="margin-top:1em">审核</div>
        <el-form v-if="!onlyBrowse" ref="leave_form" :model="form" :rules="rules" size="mini" label-position="center">
          <el-form-item prop="status" style="text-align:center">
            <el-radio-group v-model="form.status">
              <el-radio label="审核通过" value="审核通过"></el-radio>
              <el-radio label="审核不通过" value="审核不通过"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核理由：">
            <el-input type="textarea" :rows="4" v-model="form.reason"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- /抽屉 -->
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
import { mapGetters } from 'vuex'
import { generateYearOptions } from '@/utils/year' // 获取年份选项
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     onlyBrowse:false,  // 只允许查看
      applyData:{list:[]},
      applies:[],
      dialogVisible:false,
      param :{
        page:1,
        pageSize:15,
        year: new Date().getFullYear()
      },
      yearOptions: [],  // 年份选项
      visible:false,
      form:{},
      rules: {
        status: [ { required: true, message: '请选择', trigger: 'change' } ],
      },
      url:''
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['user','directionClazzTree'])
  },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    submitHandler(){
       this.$refs['leave_form'].validate((valid) => {
        if (valid) {
          let url = "/leave/checkApply";
          this.form.leaveIds = this.applies.map(item => item.id);
          post(url,this.form).then(response => {
            this.$message({ message: response.message, type: 'success' })
            this.loadApplies();
            this.visible = false
          });
        } else {
          return false
        }
      })
    },
    // 取消
    toCancelHandler(){

    },
    // 加载请假信息
    loadApplies(){
      let url = "/leave/pageQuery"
      get(url,this.param).then(resp => {
        this.applyData = resp.data;
      })
    },
    // 跳转到申请页面
    toCheckHandler(row,onlyBrowse){
      if(row){
        this.applies = [row];
      }
      this.onlyBrowse = onlyBrowse;
      this.$set(this.form,'status','审核通过')
      this.visible = true;
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadApplies();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadApplies();
    },
    handleSelectionChange(val) {
      this.applies = val;
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.loadApplies();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
    this.yearOptions = generateYearOptions();
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped lang="scss">
.photo_container {
  max-height:200px;
  overflow: scroll;
  img {
    width: 100%;
  }
}
</style>
